// import '../stylesheets/index.css'
import '../stylesheets/reset.css'
import '../stylesheets/style.css'
import '../stylesheets/index.css'
import '../javascripts/purejs'
import {default as contract} from 'truffle-contract'
import create_users from '../../build/contracts/CreateUsers.json';
var  User = contract(create_users);
var provider;
window.App = {
    start: async function () {
        var self = this;
        // 配置驱动链接至链
        User.setProvider(web3.currentProvider);
        get_owner_address();
        getAccount();
    },
};








// function getretreive() {
//   User.deployed().then(function(i) {
//     i.retreive.call().then(function(f){
//             console.log(f.words[0]);
//             let number = f.words[0];
//             document.getElementById('text2').innerText=number;
//         })
//   });
// };

//getOwnerAddress
function get_owner_address(){
    var addr = provider.selectedAddress;
    setCookie('address',addr,'s20000')
    document.getElementById('text').innerText = addr;
};

function getAccount() {
    var from_addr = document.getElementById('text').innerText;
    console.log(from_addr)
    User.deployed().then(function (i) {
        i.getAccountByAddress.call(from_addr).then(function(f){
            setCookie('account',f[3],'s20000')
            setCookie('pwd',f[1],'s20000')
        });
    });
};

window.loginInByAccount = function() {
    var from_addr = document.getElementById('text').innerText;
    var userName = document.getElementById('userName').value;
    var userPwd = document.getElementById('userPwd').value;
    var account = getCookie('account');
    var pwd = getCookie('pwd');
    User.deployed().then(function (i) {
        i.isInccount(from_addr).then(function (f) {
            if(f == true){
                if (userName == account && userPwd == pwd){
                    alert("登陆成功")
                    window.location ="chat.html";
                } else {
                    alert("账号或者密码错误！！")
                    window.location.reload();
                }
            }else {
                alert("账户不存在,请注册！！")
            }
        })
    })
}

//cookie存储简单数据
function setCookie(name,value,time)
{
    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec*1);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getsec(str)
{
    var str1=str.substring(1,str.length)*1;
    var str2=str.substring(0,1);
    if (str2=="s")
    {
        return str1*1000;
    }
    else if (str2=="h")
    {
        return str1*60*60*1000;
    }
    else if (str2=="d")
    {
        return str1*24*60*60*1000;
    }
}
//这是有设定过期时间的使用示例：
//s20是代表20秒
//h是指小时，如12小时则是：h12
//d是天数，30天则：d30

function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

    if(arr=document.cookie.match(reg))

        return unescape(arr[2]);
    else
        return null;
};


window.addEventListener('load', function() {
    // Checking if Web3 has been injected by the browser (Mist/MetaMask)
    if (typeof web3 !== 'undefined') {
        // Use the browser's ethereum provider
        provider = web3.currentProvider;
    } else {
        console.log('No web3? You should consider trying MetaMask!')
    }

    web3.eth.getAccounts(function (err, accounts) {
        try {
            if (accounts.length == 0) {
                $("#account").html("请检查钱包账户是否解锁");
            } else {
                document.getElementById('account').remove();
            }
        }
        catch (e) {
            console.log("no problem")
        }


    });
  App.start();
});


